package com.example.onenet.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.amap.api.maps.AMap;

import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.CoordinateConverter;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;

import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.example.onenet.R;

/**
 * 介绍在地图上绘制Marker,添加动画，响应事件
 */

public class MapActivity extends AppCompatActivity {


    private static final String EXTRA_GPS_COORD_LAT =
            "com.example.android.onenet.gps_coord_lat";

    private static final String EXTRA_GPS_COORD_LON =
            "com.example.android.onenet.gps_coord_lon";

    public static Intent newIntent(Context packageContext, double lat, double lon) {
        Intent intent = new Intent(packageContext, MapActivity.class);

        intent.putExtra(EXTRA_GPS_COORD_LAT, lat);
        intent.putExtra(EXTRA_GPS_COORD_LON, lon);

        return intent;
    }


    private MapView mapView;
    private AMap aMap;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);
        mapView = (MapView) findViewById(R.id.map);
        mapView.onCreate(savedInstanceState);// 此方法必须重写
        init();
    }

    /**
     * 初始化AMap对象
     */
    private void init() {
        if (aMap == null) {
            aMap = mapView.getMap();
        }

        Intent intent = getIntent();
        //
        LatLng latLng = converterCoord(intent.getDoubleExtra(EXTRA_GPS_COORD_LAT,0),intent.getDoubleExtra(EXTRA_GPS_COORD_LON,0));
        //参数依次是：视角调整区域的中心点坐标、希望调整到的缩放级别、俯仰角0°~45°（垂直与地图时为0）、偏航角 0~360° (正北方为0)
        changeCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(latLng, 16, 30, 0)));
        final Marker marker = aMap.addMarker(new MarkerOptions().position(latLng)
                .icon(BitmapDescriptorFactory
                        .defaultMarker(BitmapDescriptorFactory.HUE_RED)));
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

    /**
     * 方法必须重写
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
    }

    /**
     * 根据动画按钮状态，调用函数animateCamera或moveCamera来改变可视区域
     */
    private void changeCamera(CameraUpdate update) {

        aMap.moveCamera(update);

    }

    public LatLng converterCoord(double lat, double lon) {
        LatLng sourceLatLng = new LatLng(lat, lon);
        CoordinateConverter converter = new CoordinateConverter(this);
        // CoordType.GPS 待转换坐标类型
        converter.from(CoordinateConverter.CoordType.GPS);
        // sourceLatLng待转换坐标点 LatLng类型
        converter.coord(sourceLatLng);
        return converter.convert();
    }

}

